package com.gewu.pm.ui.activity.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.TestBean;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.ui.adapter.home.TerminalDrillingAdapter;
import com.gewu.pm.ui.dialog.SelectionListDialog;
import com.gewu.pm.ui.popup.SelectTimePopup;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.hjq.base.BasePopupWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * 终端下钻
 */
public class TerminalDrillingActivity extends AppActivity implements BaseAdapter.OnItemClickListener {
    private EditText etSearch;
    private TextView tvDistributorName;
    private RecyclerView mRecyclerView;
    private TextView tvTime;
    private TextView tvAmountSize;
    private TerminalDrillingAdapter mAdapter;
    private List<TestBean> mDataList = new ArrayList<>();

    public static void start(Context context, String name) {
        Intent intent = new Intent(context, TerminalDrillingActivity.class);
        intent.putExtra(IntentKey.NAME, name);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_terminal_drilling;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        etSearch = findViewById(R.id.et_search);
        tvDistributorName = findViewById(R.id.tv_distributor_name);
        mRecyclerView = findViewById(R.id.rv_list);
        tvTime = findViewById(R.id.tv_time);
        tvAmountSize = findViewById(R.id.tv_amount_size);
        etSearch.setHint("搜索终端名称");

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new TerminalDrillingAdapter(getContext(), getString(IntentKey.NAME));//因为在adapter中跳转，传name给第二个页面
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);

        setOnClickListener(tvTime, tvAmountSize);
    }

    @Override
    protected void initData() {
        tvDistributorName.setText(getString(IntentKey.NAME));
        loadData();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        if (id == R.id.tv_time) {//时间
            List<String> str = new ArrayList<>();
            str.add("今天");
            str.add("昨天");
            str.add("本周");
            str.add("本月");
            // 获取当前显示的文本，用于设置选中状态
            String currentText = tvTime.getText().toString();
            new SelectTimePopup.Builder(getContext())
                    .setList(str)
                    .setSelectedItem(currentText)  // 根据当前显示的文本设置选中项
                    .setListener(new SelectTimePopup.OnListener() {
                        @Override
                        public void onSelected(BasePopupWindow popupWindow, String category) {
                            tvTime.setText(category);
                        }
                    })
                    .showAsDropDown(view);

        } else if (id == R.id.tv_amount_size) {//排序
            List<String> options = new ArrayList<>();
            options.add("按发货金额高-低");
            options.add("按发货金额低-高");
            options.add("按发货数量高-低");
            options.add("按发货数量低-高");
            new SelectionListDialog.Builder(getActivity())
                    .setTitle("排序方式")
                    .setDataList(options)
                    .setListener(new SelectionListDialog.OnListener() {
                        @Override
                        public void onSelected(BaseDialog dialog, String selectedItem, int position) {
                            tvAmountSize.setText(selectedItem);
                        }
                    })
                    .show();
        }
    }

    /**
     * 加载数据（创建测试数据）
     */
    private void loadData() {
        mDataList.clear();

        mDataList.add(new TestBean("1", "万客隆"));
        mDataList.add(new TestBean("2", "十足"));
        mDataList.add(new TestBean("3", "华联"));

        mAdapter.setData(mDataList);
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {

    }
}